<?php

// +----------------------------------------------------------------------
// | Easyadmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller\apply;
use app\common\controller\AdminController;
use EasyAdmin\annotation\NodeAnotation;
use think\App;


use app\admin\model\website\LuckCate;
use app\admin\model\website\LuckRecord;
use app\admin\model\website\Configine;


/**
 * Class Admin
 * @package app\admin\controller\user
 * @ControllerAnnotation(title="站点管理")
 */
class LuckDraw extends AdminController
{

    use \app\admin\traits\Curd;

    protected $sort = [
        'id'   => 'desc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new LuckCate();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            
           

            $count = $this->model
            ->where($where)
            ->count();


            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }

        $frequency = Configine::where("ident","luck_draw_frequency")->value("value");
        $explain   = Configine::where("ident","luck_draw_explain")->value("value");

        $this->assign("frequency",$frequency);
        $this->assign("explain",$explain);
        return $this->fetch();
    }

    public function record ()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            
            $this->model = new LuckRecord();

            $countModel = $this->model;

            $listModel = $this->model;

            if (session("admin.group") != 1 && session("admin.group") != '5' && session("admin.group") != '6') {
                $countModel->where('u.extension', session("admin.username"));
            }else if (session("admin.group") != 1 && session("admin.group") != '5') {
                $countModel->where('u.rooms', 'in', explode(",", session("admin.rooms")));
                $countModel->where("u.group", "not in", "1,4,5,6");
            } else if (session('admin.group') == 5) {
                $countModel->where('u.rooms', 'in', explode(",", session("admin.rooms")));
                $countModel->where("u.group", "not in", "1,4,5");
            }

            if (session("admin.group") != 1 && session("admin.group") != '5' && session("admin.group") != '6') {
                $listModel->where('u.extension', session("admin.username"));
            }else if (session("admin.group") != 1 && session("admin.group") != '5') {
                $listModel->where('u.rooms', 'in', explode(",", session("admin.rooms")));
                $listModel->where("u.group", "not in", "1,4,5,6");
            } else if (session('admin.group') == 5) {
                $listModel->where('u.rooms', 'in', explode(",", session("admin.rooms")));
                $listModel->where("u.group", "not in", "1,4,5");
            }

            foreach ($where as &$item) {
                $item[0]= 'l.'.$item[0];
            }
            

            $count = $countModel
            ->alias("l")
            ->join('users u','u.id = l.uid')
            ->where($where)
            ->count();


            $list = $listModel
                ->alias("l")
                ->join('users u','u.id = l.uid')
                ->where($where)
                ->field("l.*")
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    public function config()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();

            if ($post['luck_draw_frequency'] != "") {
                $frequency = new Configine();
                $row       = $frequency->where("ident",'luck_draw_frequency')->find();
                if (empty($row)) {
                    $frequency->save(['ident'=>'luck_draw_frequency','value'=>$post['luck_draw_frequency']]);
                }else{
                    $row->value = $post['luck_draw_frequency'];
                    $row->save();
                }
            }
            
            if ($post['luck_draw_explain'] != "") {
                $explain = new Configine();
                $row2    = $explain->where("ident",'luck_draw_explain')->find();
    
                if (empty($row2)) {
                    $frequency->save(['ident'=>'luck_draw_explain','value'=>$post['luck_draw_explain']]);
                }else{
                    $row2->value = $post['luck_draw_explain'];
                    $row2->save();
                }
            }
            
            return app("json")->success("修改成功");

        }
    }
}
